package com.macaline.controller.user;

import com.macaline.service.user.LoginService;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户登录
 */
@Controller
@Api(tags = "用户登录相关接口")
public class LoginController {

	@Resource
	LoginService loginService;


	@RequestMapping("/index")
	public String loginSuccess(HttpServletRequest request) {
		return "frame/index";
	}
	/**
	 * 跳转登录页面
	 * @return
	 */
	@RequestMapping("/login")
	public String toLogin() {
		return "login";
	}

	/**
	 * 用户登录
	 * @param userName 用户名
	 * @param passWord 密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/loginSubmit", method = RequestMethod.POST)
	public Object loginSumbit(
			HttpServletRequest request,
			@RequestParam(value = "userName", required = false, defaultValue = "") String userName,
			@RequestParam(value = "passWord", required = false, defaultValue = "") String passWord) {
		return loginService.loginByUserNameAndPassWord(request, userName, passWord);
	}

	/**
	 * 退出登录
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		loginService.logout(request);
		return "frame/login";
	}
}
